package day24;

import org.junit.Test;

import java.io.*;
import java.util.Arrays;

//测试字符流
public class Demo3 {
    public static void main(String[] args) throws IOException {
        FileReader fr=new FileReader("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\出师表.txt");
        int n=fr.read();
        System.out.println((char) n);
        char[] ch=new char[10];
        fr.read(ch);
        System.out.println(Arrays.toString(ch));
        fr.close();

        FileWriter fw=new FileWriter("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\two.txt");
        fw.write(97); //写入ascii
        fw.write(20808); //写入unicode表示汉字
        fw.write("出师表"); //写入字符串
        fw.write("出师未捷".toCharArray());  //写入字符数组
        fw.flush();
        fw.close();

        FileReader fr2=new FileReader("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\出师表.txt");
        FileWriter fw2=new FileWriter("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\three.txt");
        char ch2[]=new char[1024];
        int len=0;
        while ((len=fr2.read(ch2))!=-1){
            fw2.write(ch2,0,len);
        }
        fw2.close();
        fr2.close();

    }
    @Test
    public void copy(){
        try (FileReader fr=new FileReader("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\出师表.txt");
             FileWriter fw=new FileWriter("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\four.txt");
             BufferedReader br=new BufferedReader(fr);
             BufferedWriter bw=new BufferedWriter(fw)){
             String str;
             while ((str= br.readLine())!=null){
                 bw.write(str);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
